export default (initialState: { resources: [string, string][], name: string }) => {
  // 在这里按照初始化数据定义项目中的权限，统一管理
  // 参考文档 https://umijs.org/docs/max/access
  const canSeeAdmin = !!(
    initialState && initialState.name !== 'dontHaveAccess'
  );
  const test = (url: string, methods: ("GET" | "PUT" | "POST" | "DELETE")[]) => {
    for (const key in initialState.resources) {
      if (Object.prototype.hasOwnProperty.call(initialState.resources, key)) {
        const [r, m] = initialState.resources[key];
        for (let i = 0; i < methods.length; i++) {
          const method = methods[i];
          if (m && m.indexOf(method) >= 0 && new RegExp(r).test(url)) {
            return true
          }
        }
      }
    }
    return false
  }
  return {
    canSeeAdmin,
    test,
    get: (url: string) => test(url, ["GET"]),
    post: (url: string) => test(url, ["POST"]),
    put: (url: string) => test(url, ["PUT"]),
    delete: (url: string) => test(url, ["DELETE"]),
    system: {
      role: {
        get: test("/api/system/role/", ["GET"]),
        post: test("/api/system/role/", ["POST"]),
        put: test("/api/system/role/", ["PUT"]),
        delete: test("/api/system/role/", ["DELETE"])
      }
    },
    sys_user: test("/api/system/user/", ["GET"]),
    sys_dept: test("/api/system/dept/", ["GET"]),
    sys_role: test("/api/system/role/", ["GET"]),
    sys_resource: test("/api/system/resource/", ["GET"]),
    sys_dict: test("/api/system/dict/", ["GET"]),
    sys_log: test("/api/system/log/", ["GET"]),
    sys_monitor: test("/api/monitor/server", ["GET"]),
    cms: test("/api/cms/", ["GET"]),
    cms_article: test("/api/cms/manager/article/", ["GET", "POST", "PUT", "DELETE"]),
    cms_article_publish: test("/api/cms/manager/article/publish/", ["GET", "POST", "PUT", "DELETE"]),
    cms_category_list: test("/api/cms/manager/category/", ["GET"]),
    cms_category_edit: test("/api/cms/manager/category/", ["POST", "PUT", "DELETE"]),
  };
};
